
/// 把 stm32f1xx-hal 的 pin 适配 embedded-hal v1.0 版本


// use core::marker::PhantomData;
use core::convert::Infallible;

use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};

use stm32f1xx_hal::gpio::{
    self, Output, Input
};


pub struct Pin<P>(pub P);


/// 定义了 Pin 引脚错误时的返回
impl<const P: char, const N: u8, Mode> ErrorType for Pin<gpio::Pin<P, N, Mode>> {
    type Error = Infallible;
}


/// 定义 OutputPin
impl<const P: char, const N: u8, Mode> OutputPin for Pin<gpio::Pin<P, N, Output<Mode>>> {

    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.0.set_high();
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.0.set_low();
        Ok(())
    }
}


impl<const P: char, const N: u8, Mode> InputPin for Pin<gpio::Pin<P, N, Input<Mode>>> {
    
    fn is_high(&mut self) -> Result<bool, Self::Error> {
        Ok(self.0.is_high())
    }

    fn is_low(&mut self) -> Result<bool, Self::Error> {
        Ok(self.0.is_low())
    }

}


impl<const P: char, const N: u8, Mode> StatefulOutputPin for Pin<gpio::Pin<P, N, Output<Mode>>> {
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        Ok(self.0.is_set_high())
    }

    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
        Ok(self.0.is_set_low())
    }
}



